<template>
  <div v-if="userRole === 'hr'" class="container">
    <hrHome></hrHome>
  </div>
  <div v-else-if="userRole === 'user'" class="container">
    <userHome></userHome>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import hrHome from './components/hrHome.vue'
import userHome from './components/userHome.vue'
import { useTodoInfo } from '@/stores/todoSrore'
import { useUserStore } from '@/stores/userStore'
import { useResumeStore } from '@/stores/resumeStore'
import { useHomeStore } from '@/stores/homeStore'

import dayjs from 'dayjs'
import server from '@/api'
import { message } from 'ant-design-vue'
import { home } from '@/api/homeApi'

const userRole = ref('')
const store = useTodoInfo()
const userStore = useUserStore()
const resumeStore = useResumeStore()
const homeStore = useHomeStore()
const getMyTodoList = async () => {
  try {
    const res = await server.getListMyHrApplicationVOByPage({
      current: store.page,
      pageSize: store.pageSize,
      userId: userStore.userAccount,
    })
    if (res.data.code === 0) {
      store.todoList = res.data.data.records
      store.total = res.data.data.total
      store.todoList.forEach(item => {
        item.createTime = dayjs(res.data.data.records.createTime).format('YYYY/MM/DD')
        switch (item.status) {
          case 1:
            item.status = '已发送'
            break
          case 2:
            item.status = '已回复'
            break
          case 3:
            item.status = '已拒绝'
            break
        }
      })
    } else {
      message.error(res.data.message)
    }
  } catch (e) {
    message.error(e)
  }
}
const getResumeList = async () => {
  try {
    const res = await server.getListProfileVOByPage({
      current: resumeStore.page,
      pageSize: resumeStore.pageSize,
    })
    if (res?.data.code === 0) {
      resumeStore.resumeList = res.data.data.records
      resumeStore.total = res.data.data.total
    } else {
      message.error(res.data.message)
    }
  } catch (e) {
    message.error(e)
  } finally {
  }
}

const getCompanyList = async () => {
  try {
    const res = await server.getCompanyList({
      current: homeStore.page,
      pageSize: homeStore.pageSize,
    })
    if (res?.data.code === 0) {
      homeStore.companyList = res.data.data.records
      homeStore.companyList.forEach(item => {
        item.imageUrl = '../../../assets/images/' + item.companyId + '.png'
      })
      homeStore.total = res.data.data.total
    } else {
      message.error(res.data.message)
    }
  } catch (error) {
    message.error(error)
  }
}
const getPositionByCompany = async () => {
  const res = await server.getPositionByCompany()
  if (res?.data.code === 0) {
    homeStore.positionByCompanyList = res.data.data
    homeStore.positionByCompanyList.forEach(item => {
      item.imageUrl = '../../../assets/images/' + item.companyId + '.png'
    })
  } else {
    message.error(res.data.message)
  }
}
onMounted(() => {
  const userStr = localStorage.getItem('user')
  let user = userStr ? JSON.parse(userStr) : null
  userRole.value = user.userRole
  if (userRole.value === 'hr') {
    getMyTodoList()
    getResumeList()
  } else if (userRole.value === 'user') {
    getCompanyList()
    getPositionByCompany()
  }
})
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  background-color: #fff;
}
</style>
